package com.Action;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import transformFactory.TransformDate;

import com.Entity.Orders;
import com.Entity.Userdata;
import com.Entity.Userhealthdata;
import com.Service.OrderService;
import com.Service.UserHealthDataService;
import com.Service.UserdataService;

public class HealthdataAction {
	
	private static final Logger logger = Logger.getLogger(HealthdataAction.class);
	
	private boolean dateFlag;
	private String result=null;
	private Userhealthdata userhealthdata;
	@Autowired
	private UserHealthDataService userHealthDataService;
	private List<Userhealthdata> userhealthdatalist=null;
	private TransformDate transformdate = new TransformDate();	
	Userhealthdata healthbean;
	private Orders myorders;
	@Autowired
	private OrderService myorderservice; 
	private List<Orders> myorderslist=null;
	private Userdata userdata;
	@Autowired
	private UserdataService userdataservice;


	public String displayHealthData() throws Exception{
		int userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		userhealthdatalist=this.userHealthDataService.findByUserIdDesc(userid);
		result="displayHealthDataSucc";
		return result;
	}
	
	public String addHealthData() throws Exception{
		int userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		userhealthdatalist=this.userHealthDataService.findByUserId(userid);		
		int listsize=userhealthdatalist.size();
		Date currentDate=new Date();
		
		if(userhealthdatalist==null||userhealthdatalist.isEmpty()){
			logger.info("不会吧。。。还没有填健康信息");	
			healthbean.setUserId(userid);
			healthbean.setDataUploadTime(currentDate);
			userdata=this.userdataservice.getById(userid);
			healthbean.setRealName(userdata.getRealName());
			this.userHealthDataService.save(healthbean);
			userhealthdatalist=this.userHealthDataService.findByUserId(userid);		
			result="addHealthDataSucc";
		}
		else{
			userhealthdata=userhealthdatalist.get(listsize-1);	
			dateFlag=transformdate.isSameDate(currentDate,userhealthdata.getDataUploadTime());
			logger.info("日期是否相同:"+dateFlag);
			
			if(dateFlag){
				logger.info("日期相同！");	
				setHealthData(currentDate);
				this.userHealthDataService.update(userhealthdata);
				result="addHealthDataSucc";
			}
			else if(!dateFlag){
				logger.info("日期不同！");
				healthbean.setUserId(userid);
				healthbean.setDataUploadTime(currentDate);
				logger.info("userid:"+userid);
				userdata=this.userdataservice.getById(userid);
				logger.info("ok!");
				healthbean.setRealName(userdata.getRealName());
				this.userHealthDataService.save(healthbean);
				userhealthdatalist=this.userHealthDataService.findByUserId(userid);		
				result="addHealthDataSucc";
			}
			else{
				result="setHealthData";
			}			
		}
		return result;
	}
	
	public String listMyOrder() throws Exception{
		int userid=Integer.parseInt(ServletActionContext.getRequest().getParameter("userid"));
		userhealthdatalist=this.userHealthDataService.findDataWithOrders(userid, (short) 1);
		logger.info(userhealthdatalist.get(0).getDiastolicPressure());
		result="listMyOrderSucc";
		return result;
	}
	
	public String displayMyOrder() throws Exception {
		int dataId=Integer.parseInt(ServletActionContext.getRequest().getParameter("dataId"));
		logger.info("OK!");
		userhealthdata=this.userHealthDataService.getById(dataId);
		userhealthdata.setDataReadState((short) 1);
		this.userHealthDataService.update(userhealthdata);
		myorderslist=this.myorderservice.findByDataId(dataId);
		myorders=myorderslist.get(0);
		myorders.setDataReadState((short) 1);
		myorderservice.update(myorders);
		logger.info(myorders.getOrderContent());
		return "displayMyOrderSucc";
	}
	
	public void setHealthData(Date uploadDate){
		float SystolicPressure=healthbean.getSystolicPressure();
		float DiastolicPressure=healthbean.getDiastolicPressure();
		float BloodSugar=healthbean.getBloodSugar();
		float UriaProtein=healthbean.getUriaProtein();
		float Weight=healthbean.getWeight();
		
		userhealthdata.setDataUploadTime(uploadDate);
		
		if(SystolicPressure!=0){
		userhealthdata.setSystolicPressure(SystolicPressure);
		}
		if(DiastolicPressure!=0){
		userhealthdata.setDiastolicPressure(DiastolicPressure);
		}
		if(BloodSugar!=0){
		userhealthdata.setBloodSugar(BloodSugar);
		}
		if(UriaProtein!=0){
		userhealthdata.setUriaProtein(UriaProtein);
		}
		if(Weight!=0){
		userhealthdata.setWeight(Weight);
		}		
	}
	
	public Userhealthdata getHealthbean() {
		return healthbean;
	}



	public void setHealthbean(Userhealthdata healthbean) {
		this.healthbean = healthbean;
	}
	
	
	public Userhealthdata getUserhealthdata() {
		return userhealthdata;
	}



	public void setUserhealthdata(Userhealthdata userhealthdata) {
		this.userhealthdata = userhealthdata;
	}



	public List<Userhealthdata> getUserhealthdatalist() {
		return userhealthdatalist;
	}


	public void setUserhealthdatalist(List<Userhealthdata> userhealthdatalist) {
		this.userhealthdatalist = userhealthdatalist;
	}
	
	public List<Orders> getMyorderslist() {
		return myorderslist;
	}

	public void setMyorderslist(List<Orders> myorderslist) {
		this.myorderslist = myorderslist;
	}

	public Orders getMyorders() {
		return myorders;
	}

	public void setMyorders(Orders myorders) {
		this.myorders = myorders;
	}

}
